$(function () {
    $.post("/grade/findAll", null, function (gradeList) {
        var str = "";
        for (var j = 0; j < gradeList.length; j++) {
            var obj = gradeList[j];
            str += `<input type="checkbox" name="gradeId" title=${obj.name} value=${obj.id}>`;

        }
        $("#gradeSpan").html(str);
        $("#updateGradeDiv").html(str);

    })
    findAll(1);
    layui.use('layer', function(){
        var layer = layui.layer;

    });
    layui.use('form', function () {

        var form = layui.form;
        //下拉框、复选框、单选框数据更新后，都需要调用render()，刷新渲染
        form.render();
    });
});

function findAll(pageNO) {
    $.post("/role/findAll", "pageNO=" + pageNO , function (pageObj) {
        var str = "";
        for (var i = 0; i < pageObj.list.length; i++) {
            var roleObj = pageObj.list[i];

            str+=`<tr><td>${roleObj.name}</td><td>
            <a href="javascript:findUpdate(${roleObj.id})" class="layui-btn layui-btn-normal">
            <i class="layui-icon layui-icon-util"></i> 修改角色权限</a>
        <a href="javascript:del(${roleObj.id})" class="layui-btn layui-btn-danger">
            <i class="layui-icon layui-icon-delete"></i> 删除</a>
        <a href="javascript:findById(${roleObj.id})" class="layui-btn layui-btn-warm">
            <i class="layui-icon layui-icon-search"></i> 查看角色权限</a></td></tr>`

        }$("#roleData").html(str);
        if (pageNO == 1) {
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: pageObj.total, //数据总数，从服务端得到
                    limit: pageObj.pageSize,
                    jump: function (obj, first) {
                        if (!first) {
                            findAll(obj.curr)
                        }
                    }
                })
            })
        };
    });
}

function del(id) {
    $.post("/role/del", "id=" + id , function () {
        findAll(1);
    })
}

function add() {
    $("#resetBtnAdd").click();
    layer.open({
        title:['添加新角色','font-size:20px','margin-left: 100px'],
        type:1,
        shadeClose:true,
        skin:'layui-layer-rim',
        area:['740px','460px'],
        anim:2,
        content:$("#addDiv")
    })
}

function addRole() {
    if(($("#roleName").val() == "" && $("#roleName").val().length==0)){
        layer.tips("<span>请输入角色名称</span>", "#roleNameError");
        return ;
    }
    $.post("/role/add",$("#addDiv").serialize(), function (info) {
        if (info=="ok"){
            layer.closeAll();
            findAll(1);
        }
        else {
            alert("添加失败，请正确完善信息");
            layer.closeAll();
        }

    })
}

function findUpdate(id) {
    $("#resetUpdateBtn").click();
    $("#id").val(id);
    layer.open({
        title:['修改角色权限','font-size:20px','margin-left: 100px'],
        type:1,
        shadeClose:true,
        skin:'layui-layer-rim',
        area:['740px','460px'],
        anim:2,
        content:$("#updateDiv")
    });
    $.post("/role/findById", "id="+id, function (roleObj) {
        $("#updateName").html(roleObj.name)
        var gradeIdArray = [];
        for (var i=0;i<roleObj.gradeList.length;i++){
            gradeIdArray[i]=roleObj.gradeList[i].id
        }
        for (var i = 0; i < gradeIdArray.length; i++) {
            $(":checkbox[name='gradeId'][value='" + gradeIdArray[i] + "']").prop("checked", true);
        }

        layui.use('form', function () {

            var form = layui.form;
            //下拉框、复选框、单选框数据更新后，都需要调用render()，刷新渲染
            form.render();
        });

    })


}

function updateGrade() {
    // alert($("#updateDiv").serialize());
    // var obj = [];
    // obj = $("#updateDiv").serialize().split("&");
    // for(var i=0;i<obj.length;i++){
    //     obj[i] = obj[i].split("=")[1];
    // }
    // alert(obj);
    if($("#updateDiv").serialize().indexOf("gradeId")!=-1) {
        $.post("/role/update", $("#updateDiv").serialize(), function () {
            layer.closeAll();
            findAll(1);
        })
    }else {
        alert("请选择一个权限")
    }

}

function findById(id) {
    layer.open({
        title:['查看角色权限','font-size:20px','margin-left: 100px'],
        type:1,
        shadeClose:true,
        skin:'layui-layer-rim',
        area:['740px','460px'],
        anim:2,
        content:$("#findByIdDiv")
    })
    $.post("/role/findById", "id="+id, function (roleObj) {
        $("#findByIdName").html(roleObj.name)
        var str="";
        for (var i=0;i<roleObj.gradeList.length;i++){

            str+=roleObj.gradeList[i].name+" "
        }
        $("#findByIdGrade").html(str)

    })

}